#include "bsp.h"



/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 168000000
  *            HCLK(Hz)                       = 168000000       //336/2 = 168M
  *            AHB Prescaler                  = 1               //AHB最大168M
  *            APB1 Prescaler                 = 4               //APB1低速时钟最大42M
  *            APB2 Prescaler                 = 2               //APB2高速时钟最大84M
  *            HSE Frequency(Hz)              = 8000000
  *            PLL_M                          = 8
  *            PLL_N                          = 336
  *            PLL_P                          = 2
  *            PLL_Q                          = 7
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale1 mode
  *            Flash Latency(WS)              = 5                //等待周期   FLASH延迟
  * 
  * Fs  外接晶振
  *VCO 频率 Fvco=Fs*(plln/pllm);
  *系统时钟频率 Fsys=Fvco/pllp=Fs*(plln/(pllm*pllp));
  *USB,SDIO,RNG 等的时钟频率 Fusb=Fvco/pllq=Fs*(plln/(pllm*pllq));
  * @param  None
  * @retval None
  */
void SystemClock_Config(void)
{
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    RCC_OscInitTypeDef RCC_OscInitStruct;

    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    RCC_OscInitStruct.OscillatorType        = RCC_OSCILLATORTYPE_HSE;       //晶振源为外部8MHz晶振
    RCC_OscInitStruct.HSEState              = RCC_HSE_ON;                   //外部高速时钟开启
    RCC_OscInitStruct.LSEState              = RCC_LSE_OFF;                  //外部低速时钟关闭
    RCC_OscInitStruct.HSIState              = RCC_HSI_OFF;                  //内部高速时钟关闭
    RCC_OscInitStruct.HSICalibrationValue   = RCC_HSICALIBRATION_DEFAULT;   //HEI默认校准

    RCC_OscInitStruct.PLL.PLLState          = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource         = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM              = 8;                            //
    RCC_OscInitStruct.PLL.PLLN              = 336;                          //PLLI2S vco的倍频系数
    RCC_OscInitStruct.PLL.PLLP              = RCC_PLLP_DIV2;                //生成高速系统时钟
    RCC_OscInitStruct.PLL.PLLQ              = 7;                            //48M时钟，用于USB OTG FS时钟，随机数发生器时钟和SDIO时钟
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
        clocks dividers */
    RCC_ClkInitStruct.ClockType         = (     RCC_CLOCKTYPE_SYSCLK 
                                            |   RCC_CLOCKTYPE_HCLK 
                                            |   RCC_CLOCKTYPE_PCLK1 
                                            |   RCC_CLOCKTYPE_PCLK2     );
    RCC_ClkInitStruct.SYSCLKSource      = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider     = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider    = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider    = RCC_HCLK_DIV2;
    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)        //5个等待周期
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);      
}

void CLK_Init(void)
{
    SystemClock_Config();
}

